package com.xxljob.demo.core.conf;

import com.xxljob.demo.core.scheduler.XxlJobScheduler;
import com.xxljob.demo.dao.XxlJobGroupDao;
import com.xxljob.demo.dao.XxlJobInfoDao;
import com.xxljob.demo.dao.XxlJobLogDao;
import com.xxljob.demo.dao.XxlJobRegistryDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Arrays;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2022/5/19 20:04
 */
@Component
public class XxlJobAdminConfig implements InitializingBean, DisposableBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(XxlJobAdminConfig.class);

    //singleton
    private static XxlJobAdminConfig adminConfig = null;

    public static XxlJobAdminConfig getAdminConfig() {
        return adminConfig;
    }

    // ---------------------- XxlJobScheduler ----------------------

    @Value("${xxl.job.triggerpool.fast.max}")
    private int triggerPoolFastMax;

    @Value("${xxl.job.triggerpool.slow.max}")
    private int triggerPoolSlowMax;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    // conf
    @Value("${xxl.job.i18n}")
    private String i18n;

    @Resource
    private XxlJobGroupDao xxlJobGroupDao;

    @Resource
    private XxlJobRegistryDao xxlJobRegistryDao;

    @Resource
    private DataSource dataSource;

    @Resource
    private XxlJobInfoDao xxlJobInfoDao;

    @Resource
    private XxlJobLogDao xxlJobLogDao;

    public String getI18n() {
        if (!Arrays.asList("zh_CN", "zh_TC", "en").contains(i18n)) {
            return "zh_CN";
        }
        return i18n;
    }

    public XxlJobLogDao getXxlJobLogDao() {
        return xxlJobLogDao;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public XxlJobInfoDao getXxlJobInfoDao() {
        return xxlJobInfoDao;
    }

    public XxlJobRegistryDao getXxlJobRegistryDao() {
        return xxlJobRegistryDao;
    }

    public String getAccessToken() {
        return accessToken;
    }


    public XxlJobGroupDao getXxlJobGroupDao() {
        return xxlJobGroupDao;
    }

    public int getTriggerPoolFastMax() {
        if (triggerPoolFastMax < 200) {
            return 200;
        }
        return triggerPoolFastMax;
    }

    public int getTriggerPoolSlowMax() {
        if (triggerPoolSlowMax < 100) {
            return 100;
        }
        return triggerPoolSlowMax;
    }

    // ---------------------- XxlJobScheduler ----------------------
    private XxlJobScheduler xxlJobScheduler;

    @Override
    public void afterPropertiesSet() throws Exception {
        LOGGER.info("XxlJobAdminConfig afterPropertiesSet bean属性后置方法");
        adminConfig = this;
        xxlJobScheduler = new XxlJobScheduler();
        xxlJobScheduler.init();
    }

    @Override
    public void destroy() throws Exception {
        LOGGER.info("destroy方法");
        xxlJobScheduler.destroy();
    }
}
